using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Collections;

namespace OvernightForInterestToCommCost
{
    public partial class frmImChargeInterestToCommCost : Form
    {

        private string mstrFieldName = "C:\\Program Files\\WoodPro Software Inc\\WoodPro InSight" + "\\ImDailyInterestToCommCost.XmL";
            
        public frmImChargeInterestToCommCost()
        {
            InitializeComponent();
        }            
   
        private void LoadConfigTxt()
        {
            bool blnNew = false ;
            string strFileName = "";
            string strPathName = "";

            strPathName = mstrFieldName.Replace("\\ImDailyInterestToCommCost.XmL", "");

            if (!Directory.Exists(strPathName))
            {
                strPathName = strPathName.Replace("Program Files", "Program Files (x86)");

                if (!Directory.Exists(strPathName))
                {
                    return;
                }
            }

            strFileName = strPathName + "\\ImDailyInterestToCommCost.XmL";
            mstrFieldName = strFileName;

            if (!File.Exists(strFileName)) 
            {
                 blnNew = true;
                 return;
 
            }
            
             DataTable dtTable = new DataTable();
             dtTable.ReadXml(strFileName);
             foreach (DataRow drRow in dtTable.Rows)
             {
                 
                 txtRunTime.Text = drRow["StartTime"].ToString();
                 ArrayList arrListServerAndDB= new ArrayList ();
                 string strUserName = string.Empty;
                 string strPwd = string.Empty;
                 AnalysConStr(drRow["ConnectString"].ToString(), ref  arrListServerAndDB, ref strUserName, ref strPwd);
                 foreach ( string strServerAndDB in arrListServerAndDB )
                 {
                     if ( txtServerName.Text =="")
                        txtServerName.Text = strServerAndDB  ; 
                     else
                        txtServerName.Text +="\r\n" + strServerAndDB; 

                 }
                 txtUserName.Text = strUserName;
                 txtPWD.Text = strPwd;
             }            
        }

        private void AnalysConStr(string strConStr, ref ArrayList arrListServerAndDB, ref string strUserName, ref string strPwd)
        {
            //data source=Boserver1\SQL2005;database=RTBP20090902;Connect Timeout=90;uid=procom;pwd=2W00dpr00;
            string[] strServerandDB = strConStr.Split(new string[] { "\r\n" } , StringSplitOptions.RemoveEmptyEntries);
            if (strConStr != "" && strServerandDB.Length == 0)
            {

            }
            else
                for (int i = 0; i < strServerandDB.Length; i++)
                {
                    string strServerName = strServerandDB[i].Split(new char[] { ';' })[0];
                    strServerName = strServerName.Replace("data source=", "");
                    string strDBName = strServerandDB[i].Split(new char[] { ';' })[1];
                    strDBName = strDBName.Replace("database=", "");
                    arrListServerAndDB.Add(strServerName + ";" + strDBName);

                    strUserName = strServerandDB[i].Split(new char[] { ';' })[3];
                    strUserName = strUserName.Replace("uid=", "");
                    strPwd = strServerandDB[i].Split(new char[] { ';' })[4];
                    strPwd = strPwd.Replace("pwd=", "");
                }
        }
        
        private bool VaildCheck()
        {            
            if (txtServerName.Text == "")
            {
                MessageBox.Show("Please set the server and database name.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            try
            {
                string[] strServerandDB = txtServerName.Text.Split(new string[] { "\r\n" } , StringSplitOptions.RemoveEmptyEntries);
                if ((strServerandDB.Length) <= 0)
                {
                }
                else
                {
                    for (int i = 0; i < strServerandDB.Length; i++)
                    {
                        string strServerName = strServerandDB[i].Split(new char[] { ';' })[0];
                        string strDBName = strServerandDB[i].Split(new char[] { ';' })[1];
                        string strConDB = "data source=" + strServerName + ";database=" + strDBName + ";Connect Timeout=20;Trusted_Connection=yes;";
                        try
                        {
                            DBAccess.ConnectionDBString = strConDB;
                            DBAccess dbcon = new DBAccess(false, false);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Please reset servername and database.", "" ,MessageBoxButtons.OK, MessageBoxIcon.Warning  );
                            return false;
                        }
                    }
                }   
            }
            catch (Exception ex)
            {
                return false;
            }

            if (txtRunTime.Text == "")
            {
                MessageBox.Show("Please set the Overnight Run Time", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            try
            {

                DateTime dt;
                if (DateTime.TryParse(txtRunTime.Text, out dt))
                {
                    return true;
                }
                else
                {
                    MessageBox.Show("Please reinput the Run Time.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }
            catch ( Exception ex)
            {
                MessageBox .Show ( "Please reinput the Run Time.","", MessageBoxButtons .OK , MessageBoxIcon .Warning );
                return false ;
            }

            return true;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (UpdateData())
            {
                this.Close();
                Application.Exit();
            }
        }
        
        private bool UpdateData()
        {
            if (!VaildCheck()) return false;            
            if (txtServerName.Text == "")
            {
                MessageBox.Show("Please input the server name and database name.");
            }
            else
            {
                string[] strServerandDB = txtServerName.Text.Split(new string[] { "\r\n" } , StringSplitOptions.RemoveEmptyEntries);
                if ((strServerandDB.Length) <= 0)
                {
                }
                else
                {
                    string strConDB = string.Empty;
                    for (int i = 0; i < strServerandDB.Length; i++)
                    {
                        string strServerName = strServerandDB[i].Split(new char[] { ';' })[0];
                        string strDBName = strServerandDB[i].Split(new char[] { ';' })[1];
                        if (strConDB == "")
                            strConDB = @"data source=" + strServerName + ";database=" + strDBName + ";Connect Timeout=90;uid=" + txtUserName.Text + ";pwd=" + txtPWD.Text + ";";
                        else
                            strConDB += "\r\n" + @"data source=" + strServerName + ";database=" + strDBName + ";Connect Timeout=90;uid=" + txtUserName.Text + ";pwd=" + txtPWD.Text + ";";
                    }
                    DataTable dtDailyInterestToCommCost = new DataTable("ImDailyInterestToCommCost");
                    dtDailyInterestToCommCost.Columns.Add(new DataColumn("ConnectString", typeof(string)));
                    dtDailyInterestToCommCost.Columns.Add(new DataColumn("StartTime", typeof(string)));
                    
                                        
                    dtDailyInterestToCommCost.Rows.Add();
                    DataRow dr = dtDailyInterestToCommCost.Rows[0];
                    dr["ConnectString"] = strConDB  ;
                    dr["StartTime"] =  txtRunTime.Text  ;
                   
                    dtDailyInterestToCommCost.AcceptChanges();
                    if (File.Exists(mstrFieldName)) File.Delete(mstrFieldName);  
                    dtDailyInterestToCommCost.WriteXml(mstrFieldName, XmlWriteMode.WriteSchema, false);
                    
                }
            }
            return true;
        }
        
        private void btnRun_Click(object sender, EventArgs e)
        {

               
        }

        private void frmImChargeInterestToCommCost_Load(object sender, EventArgs e)
        {
            LoadConfigTxt();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }
    }
}
